import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { LoginReq } from 'src/model/account/LoginReq';
import { AccountAPI } from 'src/api/AccountAPI';
import { MsgboxUtil } from 'src/helper/MsgboxUtil';
import { ComHelper } from 'src/helper/comHelper';
import { StoreHelper } from 'src/helper/StoreHelper';

@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss']
})
export class LoginPage implements OnInit {
  req: LoginReq = new LoginReq(); //登录条件
  isSavePwd = false; //是否保存密码

  constructor(
    private router: Router,
    private accountAPI: AccountAPI,
    private msgBox: MsgboxUtil
  ) {}

  ngOnInit() {
    this.req.password = '';
  }

  //登录
  async logIn() {
    //验证
    if (this.checkInput() === false) {
      return false;
    }

    //登录条件加密
    const reqLogin = new LoginReq();
    reqLogin.password = this.req.password;

    //请求登录接口
    const res = await this.accountAPI.login(reqLogin);
    if (res.isOK) {
      //登录成功
      //设置token
      StoreHelper.setSession("token", res.info);

      //跳转到tabs页
      this.router.navigate(['']);
    } else {
      //登录失败
      this.msgBox.infoDanger(res.msg);
    }
  }

  //验证
  checkInput() {
    if (ComHelper.isEmptry(this.req.password)) {
      this.msgBox.infoDanger('请输入密码');
      return false;
    }
    return true;
  }
}
